import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    meta: { title: '登录页' },
    name: 'login',
    component: () =>
      import(/* webpackChunkName: "login" */ '@/views/BlogLogin.vue'),
  },
  {
    path: '/home',
    meta: { title: '主页' },
    name: 'home',
    component: () =>
      import(/* webpackChunkName: "home" */ '@/views/BlogHome.vue'),
    children: [
      // 首页
      {
        path: 'main',
        name: 'main',
        meta: { title: '首页' },
        component: () =>
          import(/* webpackChunkName: "main" */ '@/views/home/HomeMain.vue'),
      },
      // 个人中心
      {
        path: 'center',
        name: 'center',
        meta: { title: '个人中心' },
        component: () =>
          import(
            /* webpackChunkName: "center" */ '@/views/home/HomeCenter.vue'
          ),
      },
      // 图书管理
      {
        path: 'article',
        name: 'article',
        meta: { title: '图书管理' },
        component: () =>
          import(
            /* webpackChunkName: "article" */ '@/views/home/HomeArticle.vue'
          ),
        children: [
          {
            path: 'article-push',
            name: 'article-push',
            meta: { title: '发布图书' },
            component: () =>
              import(
                /*webpackChunkName: "article-push" */ '@/views/article/ArticlePush.vue'
              ),
          },
          {
            path: 'article-list',
            name: 'article-list',
            meta: { title: '管理图书' },
            component: () =>
              import(
                /*webpackChunkName: "article-list" */ '@/views/article/ArticleList.vue'
              ),
          },
          {
            path: 'article-classify',
            name: 'article-classify',
            meta: { title: '图书分类' },
            component: () =>
              import(
                /*webpackChunkName: "article-classify" */ '@/views/article/ArticleClassify.vue'
              ),
          },
          {
            path: '/home/article',
            redirect: '/home/article/article-list',
          },
        ],
      },
      // 后台管理
      {
        path: 'admin',
        name: 'admin',
        meta: { title: '后台管理' },
        component: () =>
          import(/*webpackChunkName: "admin" */ '@/views/home/HomeAdmin.vue'),
        children: [
          {
            path: 'admin-main',
            name: 'admin-main',
            meta: { title: '超级管理' },
            component: () =>
              import(
                /*webpackChunkName: "admin-main" */ '@/views/admin/AdminMain.vue'
              ),
          },
          {
            path: 'admin-else',
            name: 'admin-else',
            meta: { title: '普通管理' },
            component: () =>
              import(
                /*webpackChunkName: "admin-else" */ '@/views/admin/AdminElse.vue'
              ),
          },
          {
            path: '/home/admin',
            redirect: '/home/admin/admin-else',
          },
        ],
      },
      // 动态管理
      {
        path: 'talk',
        name: 'talk',
        meta: { title: '动态管理' },
        component: () =>
          import(/*webpackChunkName: "talk" */ '@/views/home/HomeTalk.vue'),
        children: [
          {
            path: 'talk-push',
            name: 'talk-push',
            meta: { title: '发表动态' },
            component: () =>
              import(
                /*webpackChunkName: "talk-push" */ '@/views/talk/PushTalk.vue'
              ),
          },
          {
            path: 'talk-list',
            name: 'talk-list',
            meta: { title: '普通管理' },
            component: () =>
              import(
                /*webpackChunkName: "talk-list" */ '@/views/talk/TalkList.vue'
              ),
          },
          {
            path: '/home/talk',
            redirect: '/home/talk/talk-list',
          },
        ],
      },
      // 前端页面管理
      {
        path: 'page',
        name: 'page',
        meta: { title: '页面管理' },
        component: () =>
          import(/*webpackChunkName: "page" */ '@/views/home/HomePage.vue'),
        children: [
          {
            path: 'page-front',
            name: 'page-front',
            meta: { title: '首页管理' },
            component: () =>
              import(
                /*webpackChunkName: "page-front" */ '@/views/page/FrontHome.vue'
              ),
          },
          {
            path: 'page-center',
            name: 'page-center',
            meta: { title: '个人页面' },
            component: () =>
              import(
                /*webpackChunkName: "page-center" */ '@/views/page/FrontCenter.vue'
              ),
          },
          {
            path: '/home/page',
            redirect: '/home/page/page-front',
          },
        ],
      },
      // 前端用户配置
      {
        path: 'font-user',
        name: 'font-user',
        meta: { title: '用户管理' },
        component: () =>
          import(
            /* webpackChunkName: "font-user" */ '@/views/frontUser/UserManage.vue'
          ),
      },
      // 前端评论管理
      {
        path: 'comment',
        name: 'comment',
        meta: { title: '评论管理' },
        component: () =>
          import(
            /* webpackChunkName: "comment" */ '@/views/comment/Comment.vue'
          ),
      },
      // 留言管理
      {
        path: 'message',
        name: 'message',
        meta: { title: '留言管理' },
        component: () =>
          import(
            /* webpackChunkName: "message" */ '@/views/message/MessageManagement.vue'
          ),
      },
      {
        path: '/home',
        redirect: '/home/main',
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/login',
  },
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
})
router.afterEach((to, from) => {
  // 每次切换的时候切换标题名字用到了路由元传参
  document.title = (to.meta.title as string) || '小王图书管理'
})
export default router
